<?php
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Redirect;
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
/**
 * 圈子管理
 * @author Wesley
 *
 */
class CircleController extends BaseAdminController{
	public function index()
	{
		$query = App::make('SNSGroup');
		$search_name = Input::get('group_name');
		if($search_name != ''){
			$query = $query->where('group_name', 'LIKE', '%'.$search_name.'%');
		}
		$query = $query->orderBy('is_recommended', 'desc')->orderBy('updated_at','desc');
		$list = $query->paginate(Config::get('app.page_row'));
		$this->layout->content = View::make('admin.circle.index')->with(compact('list', 'search_name'));
	}
	
	public function edit($id)
	{
		if (Input::server('REQUEST_METHOD') == 'POST') {
			$data = Input::all();
			unset($data['_token']);
			if( $img = Input::file('group_avatar') )
			{
				$path = Config::get('app.upload_dir');
				$dir = '/info'.date("/Y/m/", time());
				$upload_result = UploadHelper::uploadImage('group_avatar', $path.$dir);
	            if ($upload_result['error'] == 0) {
	            } else {
	                throw new Exception('上传图片失败。');
	            }
				$data['group_avatar'] = $upload_result['files'][0]['path'];
			}
			$circle = SNSGroup::where('id', $id)->firstOrFail();
			foreach ($data as $k=>$v){
				$circle->$k = $v;
			}
			$circle->save();
			return Redirect::route('admin.circle.index');
		}else {
			$circle = SNSGroup::where('id', $id)->firstOrFail();
			$this->layout->content = View::make('admin.circle.edit')->with( compact('circle'));
		}
		
	}
	
	public function remove($id)
	{
		SNSGroup::where('id', $id)->delete();
		Post::where('post_group_id', $id)->delete();
		return Redirect::back();
	}
	
	public function members($id)
	{
		$circle = SNSGroup::where('id', $id)->firstOrFail();
		$members = SNSGroupMember::where('group_id', $id)->orderBy('is_admin','DESC')->orderBy('is_creator','DESC')->paginate(Config::get('app.page_row'));;
		$this->layout->content = View::make('admin.circle.members')->with( compact('circle', 'members','id'));
	}
	public function setcreator($id, $user_id)
	{
		$gmem = SNSGroupMember::where('group_id', $id)->where('user_id', $user_id)->firstOrFail();
		$gmem->is_creator = abs($gmem->is_creator -1);
		$gmem->save();
		return Redirect::back();
	}
	public function setadmin($id, $user_id)
	{
		$gmem = SNSGroupMember::where('group_id', $id)->where('user_id', $user_id)->firstOrFail();
		$gmem->is_admin = abs($gmem->is_admin -1);
		$gmem->save();
		return Redirect::back();
	}
	public function delmember($id, $user_id)
	{
		$gmem = SNSGroupMember::where('group_id', $id)->where('user_id', $user_id)->delete();
		return Redirect::back();
	}
	
	public function posts($id)
	{
		$circle = SNSGroup::where('id', $id)->firstOrFail();
		$posts = Post::where('type', Post::TYPE_GROUP_POST)->where('post_group_id', $id)->paginate(Config::get('app.page_row'));
		$cateList = Config::get('lst.categories');
		$this->layout->content = View::make('admin.circle.posts')->with( compact('circle', 'posts','id', 'cateList'));
	}
	public function setrecommend($id, $pid)
	{
		$post = Post::where('id', $pid)->firstOrFail();
		$post->is_recommend = abs($post->is_recommend -1);
		$post->save();
		return Redirect::back();
	}
	public function settop($id, $pid)
	{
		$post = Post::where('id', $pid)->firstOrFail();
		$post->is_top = abs($post->is_top -1);
		$post->save();
		return Redirect::back();
	}
	public function delpost($id, $pid)
	{
		Post::find($pid)->delete();
		return Redirect::back();
	}
	public function postDetail($id, $pid)
	{
		$circle = SNSGroup::find($id);
		$post = Post::find($pid);
		$comments = Comment::where('post_id',$pid)->get();
		$this->layout->content = View::make('admin.circle.post_detail')->with( compact('circle','post','comments'));
	}
}